package servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.FileDao;

/**
 * Servlet implementation class DownloadServlet
 */
public class DownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public DownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	OutputStream out = response.getOutputStream();
   	 
    	int id = Integer.parseInt(request.getParameter("id"));
    	
    	//String filename = URLDecoder.decode(url.substring(3, url.length()), "utf-8");
    	// 文本从Brower传递是被编码为iOS-8859-1，需要转换成utf-8
    	String filename_utf = null;
		try {
			filename_utf = (new FileDao()).getFilePath(id);
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}//new String(filename.getBytes("ISO-8859-1"),"utf-8");
    	
    	//String musicName = URLDecoder.decode(request.getParameter("musicname"), "utf-8");
    	//String songer = URLDecoder.decode(request.getParameter("singer"), "utf-8");

    	if (filename_utf == null || filename_utf.equals(""))
    	{
    	     out.write("请输入文件名！".getBytes("utf-8"));
    	     response.setContentType("text/html;charset=utf-8");
    	     out.close();
    	     return;
    	}
    	// 获得服务器中文件的真实存储路径
    	String filePath = getServletContext().getRealPath("WEB-INF/upload/"+filename_utf);
    	//System.out.println(filePath);
    	 
    	int indexIndector = filename_utf.indexOf(".");
    	// 获得提取文件的文件后缀，包括.号
    	String indector  = filename_utf.substring(indexIndector, filename_utf.length());
    	//String saveName = new String((musicName+" - "+songer+indector).getBytes("utf-8"), "ISO-8859-1");
    	// 由文件的全路径获得要下载文件的InputStream
    	System.out.println(filePath);
    	InputStream is = new FileInputStream(new File(filePath));
    	 
    	Date date = new Date();
    	long time = date.getTime();
    	// 设置响应头的MIME类型
    	response.setContentType("application/force-download");
    	response.setHeader("Content-Length",String.valueOf(is.available()));
    	//response.setHeader("Content-Disposition", "attachment;filename=" + saveName);
    	 
    	int i = 0;
    	byte[] b = new byte[1024*1024];
    	while ((i = is.read(b)) != -1)
    	{
    	     out.write(b, 0, i);
    	}
    	out.flush();
    	is.close();
    	out.close();

	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
